package com.shuan.reader;

import java.io.IOException;
import java.io.InputStream;
import org.springframework.asm.ClassReader;

public class DecryptedClassReader extends ClassReader {
    private static boolean isValidClassFormat(byte[] classBytes) {
        if (classBytes == null || classBytes.length < 4) {
            return false;
        }
        // Java 类文件的魔数是前4字节：0xCAFEBABE
        return (classBytes[0] & 0xFF) == 0xCA
            && (classBytes[1] & 0xFF) == 0xFE
            && (classBytes[2] & 0xFF) == 0xBA
            && (classBytes[3] & 0xFF) == 0xBE;
    }
    // 可选：重写 readClass 方法，添加解密逻辑
//    @Override
//    public void readClass(InputStream inputStream) throws IOException {
//        // 1. 读取原始加密字节码
//        byte[] encryptedBytes = readFully(inputStream);
//
//        // 2. 解密字节码（调用你的 Decryptor）
//        byte[] decryptedBytes = Decryptor.decrypt(encryptedBytes);
//
//        // 3. 使用解密后的字节码构造 ClassReader
//        super.readClass(new ByteArrayInputStream(decryptedBytes));
//    }
//
//    private byte[] readFully(InputStream inputStream) throws IOException {
//        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
//        int nRead;
//        byte[] data = new byte[1024];
//        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
//            buffer.write(data, 0, nRead);
//        }
//        buffer.flush();
//        return buffer.toByteArray();
//    }




    public DecryptedClassReader(InputStream inputStream) throws IOException {
        super(inputStream);
    }

}
